#include <bmpreader.h>
#include <stdio.h>
#include <stdlib.h>
#include <codelets.hpp>
#include <cuda.h>

// GPU Version

int main()
{
    if (argc < 2)
    {
        printf("usage: %s filename\n", argv[0]);
        exit(1);
    }

	int w, h;
	bmpreader image(argv[1]);
	image.readBMP();				// read the BMP file
	image.writeBMP("before.bmp");	// write the before picture (same file)
	
	w = image.getWidth();
	h = image.getHeight();
    float caoKernel[9] = { -1/6.0f, -4.0f/6.0, -1.0/6.0f,
                            -4/6.0f, 26/6.0f, -4/6.0f,
                            -1/6.0f, -4/6.0f, -1/6.0f };

	// host memory allocation
	BYTE *input		= (BYTE *) image.getImageData();
	BYTE *output	= (BYTE *) malloc (sizeof(BYTE)*w*h);
	memcpy(output, input, sizeof(BYTE)*w*h);

	// device memory allocation
	BYTE *input_dev, *output_dev;
	cudaMalloc((void **)input_dev, sizeof(BYTE)*w*h);
	cudaMalloc((void **)output_dev, sizeof(BYTE)*w*h);

	// copy host data to device data
	cudaMemcpy(input_dev, input, sizeof(BYTE)*w*h, cudaMemcpyHostToDevice);
	cudaMemcpy(output_dev, output, sizeof(BYTE)*w*h, cudaMemcpyHostToDevice);	

	convolve2D(w, h, input, output, caoKernel);

	image.setImageData(output, sizeof(BYTE)*w*h);
	image.writeBMP("after.bmp");

//	printf("Total Time: %f ms\n", t.getElapsedTimeInMilliSec());
	free (output);
}
